package com.javagroup.chessonline.server;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JFrame;

import com.javagroup.chessonline.chessman.ChessData;
import com.javagroup.chessonline.chessman.Chessman;



public class Server implements ActionListener ,Runnable{

	private JFrame frame;
	private  ArrayList<Worker> listWorker = new ArrayList<Worker>(); 
	public ArrayList<Worker> getListWorker() {
		return listWorker;
	}

	public void setListWorker(ArrayList<Worker> listWorker) {
		this.listWorker = listWorker;
	}

	private InputStream is;
	ServerSocket ss;
	Socket client;
	BufferedReader br;
	// data
	int currentTurn = -1;
	Chessman[] arrayChess;
	public Chessman[] getArrayChess() {
		return arrayChess;
	}

	public void setArrayChess(Chessman[] arrayChess) {
		this.arrayChess = arrayChess;
	}

	public int getCurrentTurn() {
		return currentTurn;
	}

	public void setCurrentTurn(int currentTurn) {
		this.currentTurn = currentTurn;
	}

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Server window = new Server();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 * @throws IOException 
	 */
	public Server() throws IOException {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 * @throws IOException 
	 */
	private void initialize() throws IOException  {
		frame = new JFrame();
		//frame.setSize(new Dimension(800, 400));
		frame.setResizable(false);
		frame.setBounds(0,0,100,100);
		frame.setTitle("Server");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		
		JButton btnTest = new JButton("test");
		btnTest.setActionCommand("btStart");
		btnTest.addActionListener(this);
		
		btnTest.setBounds(72, 113, 89, 23);
		frame.getContentPane().add(btnTest);
		
		new Thread(this).start();
	}


	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if(e.getActionCommand().equals("btStart")){
			new Thread(this).start();
		}
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		System.out.println("start server");
		
			int count = 0;
			try {
				ss = new ServerSocket(3579);
				
				while(true){
					client = ss.accept();
					Worker worker = new Worker(client,this,count);
					listWorker.add(worker);
					new Thread(worker).start();
					count ++;
				}
			
				
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
	}
	public void sendString(String s){
		for (Worker worker : listWorker) {
			worker.sendStringData(s);

		}
		
	}
	public void sendObject(ChessData data){
		for (Worker worker : listWorker) {
			worker.sendObject(data);

		}
		
	}

	public void sendArrayChessData(Chessman[] temp) {
		// TODO Auto-generated method stub
		for (Worker worker : listWorker) {
			worker.sendArrayChessData(temp);

		}
	}
	public void sendChessData(Chessman[] data,int id) {
		// TODO Auto-generated method stub
		for (Worker worker : listWorker) {
			if(worker.getId() != id){
				System.out.println("send den id la : "+worker.getId());
				/*worker.sendStringData("Refresh board");
				worker.sendObject(data);*/
				worker.sendObject(new ChessData("Refresh board", data));
			}


		}
	}

	public void sendNextTurn() {
		if(currentTurn == 0){
			currentTurn = 1;
		}
		else{
			currentTurn = 0;
		}
		for (Worker worker : listWorker) {
			System.out.println("turn moi la : " + String.valueOf(currentTurn));
//			worker.sendStringData(String.valueOf(currentTurn));
			worker.sendObject(new ChessData(String.valueOf(currentTurn)));
		}
		
	}


}
